#ifndef SLAMWIDGET_H
#define SLAMWIDGET_H

#include "models/robot.h"
#include "qtimer.h"
#include "scalelabel.h"
#include "touchbutton.h"
#include <QLabel>
#include <QPushButton>
#include <QScrollBar>
#include <QSlider>
#include <QWidget>
#include <qgesture.h>

class SlamWidget : public QWidget
{
    Q_OBJECT
public:
    explicit SlamWidget(QWidget *parent = nullptr);

    void setShowMax(bool newShowMax);

signals:
public slots:
    void onMapChanged();
    void onInertiaMove();
    void onMouseTimer();
protected:
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void paintEvent(QPaintEvent *event);
    void wheelEvent(QWheelEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void resizeEvent(QResizeEvent *event);
    bool event(QEvent *event);
    //设置比例尺
    void reSetScale();

    QPointF ui2real(QPoint pt);
    QPoint  real2ui(QPointF pt);
    void bindRobot();

private:
    //控件
    QWidget * m_pCtrlWidget;
    //比例尺
    ScaleLabel * m_pScale;
    //放大按钮
    TouchButton * m_pZoomInBtn;
    //缩小按钮
    TouchButton * m_pZoomOutBtn;
    //缩放滑块
    QSlider * m_pZoomSlider;
    //导航按钮
    TouchButton * m_pNavigateBtn;
    //测距按钮
    TouchButton * m_pRangBtn;
    //设置机器人起始位置按钮
    TouchButton * m_pSetStartBtn;
    //机器人居中按钮
    TouchButton * m_pCenterBtn;

    //机器人图标
    QLabel * m_pRobot;

    //测距点
    QLabel * m_pRangStart;
    QPointF m_ptRangStart;
    QLabel * m_pRangEnd;
    QPointF m_ptRangEnd;
    QLabel * m_pRangLab;
    bool m_bDragRangStart;
    bool m_bDragRangEnd;

    TouchButton * m_pCancelRangBtn;

    QLabel * m_pDest;
    QPointF m_ptDest;
    float m_fDestAngle;
    TouchButton * m_goDestBtn;
    bool m_bToDest; //自己到达了终点

    //图片
    QPixmap m_pixMap;
    //上次鼠标点击位置
    QPoint m_ptLastMouse;
    //图片偏移量
    QPoint m_ptOffset;
    //正在拖动
    bool m_bDragging;
    //缩放倍数
    double m_scaleFactor;
    //滑动相关
    //定时器
    QTimer m_inertiaTimer;
    //x方向滑动速度
    double m_xMove;
    //y方向滑动速度
    double m_yMove;

    //用于需要滚动时检测鼠标位置
    QTimer m_MouseTimer;

    //当前绑定的机器人
    Robot * m_pCurRobot;

    enum MAP_MODE{
        MAP_ERROR = 0, //没获取到地图
        MAP_MINSIZE, //小窗口模式
        MAP_NORMAL, //正常模式
        MAP_NAVIGATE, //导航
        MAP_NAVIGATING, //正在导航
        MAP_SETROBOT, //设置机器人初始位置
        MAP_RANG //测距

    };
    MAP_MODE m_Mode;

    bool m_bShowMax;

    qreal m_last2TouchDistance = 0;

    // QWidget interface
protected:
    void showEvent(QShowEvent *event);
    void hideEvent(QHideEvent *event);
    void setRobotPosition(QPointF ptReal);
    void setRobotRotation(float nAngle);
    void setScaleFactor(QPoint ptCenter,double newScaleFactor);
    void setOffSet(int x, int y);
    void updateRang(QPoint ptStart, QPoint ptEnd);

    void resetCtrlPos();
};

#endif // SLAMWIDGET_H
